﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace dreametryVersion2
{
    public partial class ucComments : UserControl
    {
        public ucComments()
        {
            InitializeComponent();
        }

        DataTable data;
        int count;
        SqlDataReader reader;
        int taskid;
        List<TextBox> commentsmssg;

        public void getComment(int id)
        {
            pnlMain.Controls.Clear();
            commentsmssg = new List<TextBox>();
            taskid = id;
            //initialize database
            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                //sql command and fill data to datatable
                SqlCommand command = new SqlCommand("SELECT * FROM ViewComments WHERE task_id = @id ORDER BY comment_date ASC", connection);
                command.Parameters.AddWithValue("@id", id);
                command.Connection.Open();

                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    data = new DataTable();
                    data.Clear();
                    adapter.Fill(data);
                    reader = command.ExecuteReader();
                    count = data.Rows.Count;
                }
                catch
                {
                }

            }

            //   MessageBox.Show(count.ToString());

            if (count >= 1)
            {
                pnlMain.Visible = false;
                for (int i = 0; i < count; i++)
                {

                    Panel pnlholder = new Panel()
                    {
                        Dock = DockStyle.Top,
                        Padding = new Padding(0, 10, 0, 10),
                        Height = 200
                    };


                    Panel pnlcomment = new Panel()
                    {
                        Dock = DockStyle.Fill,
                        Padding = new Padding(10, 10, 10, 10),
                        BackColor = Color.WhiteSmoke,
                    };


                    Panel pnlimage = new Panel()
                    {
                        Dock = DockStyle.Left,
                        Padding = new Padding(5,0,5,0),
                        Width = 60
                    };


                    PictureBox picbox = new PictureBox()
                    {
                        Size = new Size(50, 50),
                        Dock = DockStyle.Top,
                        SizeMode = PictureBoxSizeMode.Zoom
                    };
                    Byte[] datas = new Byte[0];
                    datas = (Byte[])(data.Rows[i][0]);
                    MemoryStream mem = new MemoryStream(datas);
                    picbox.Image = Image.FromStream(mem);


                    TextBox comment = new TextBox()
                    {
                        Text = data.Rows[i][2].ToString(),
                        Dock = DockStyle.Fill,
                        BackColor = Color.WhiteSmoke,
                        BorderStyle = BorderStyle.None,
                        ReadOnly = true,
                        Multiline = true
                    }; commentsmssg.Add(comment);

                    commentsmssg[i].MouseLeave += UcComments_MouseLeave;

                    Label date = new Label()
                    {
                        Text = data.Rows[i][3].ToString(),
                        Dock = DockStyle.Bottom,
                        TextAlign = ContentAlignment.BottomRight
                    };

                    Label name = new Label()
                    {
                        Text = data.Rows[i][1].ToString(),
                        Dock = DockStyle.Top,
                        TextAlign = ContentAlignment.TopLeft,
                        Font = new Font(this.Font, FontStyle.Bold)
                    };


                    //<height>
                    Size sz = new Size(comment.ClientSize.Width, int.MaxValue);
                    TextFormatFlags flags = TextFormatFlags.WordBreak;
                    int padding = 3;
                    int borders = comment.Height - comment.ClientSize.Height;
                    sz = TextRenderer.MeasureText(comment.Text, comment.Font, sz, flags);
                    int h = sz.Height + borders + padding;
                    if (comment.Top + h > this.ClientSize.Height - 10)
                    {
                        h = this.ClientSize.Height - 10 - comment.Top;
                    }
                    comment.Height = h;
                    //</height>
                    
                    pnlholder.Height = comment.Height + 100;

                    pnlcomment.Controls.Add(comment);
                    pnlcomment.Controls.Add(name);

                    pnlcomment.Controls.Add(date);

                    pnlimage.Controls.Add(picbox);
                    pnlholder.Controls.Add(pnlcomment);
                    pnlholder.Controls.Add(pnlimage);

                    pnlMain.Controls.Add(pnlholder);

                }
                pnlMain.Visible = true;
                pnlMain.Focus();
            }
            else
            {
                Label comment = new Label()
                {
                    Text = "Be the first to comment",
                    Dock = DockStyle.Fill,
                    TextAlign = ContentAlignment.TopCenter,
                    Padding = new Padding(10)
                };
                pnlMain.Controls.Add(comment);
            }


        }

        private void UcComments_MouseLeave(object sender, EventArgs e)
        {
            pnlMain.Focus();
        }

        private void ucComments_Load(object sender, EventArgs e)
        {

        }


    }
}
